package com.sgb.kjwl.view.ui.activity.self.logistics;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityBargainingBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.BargainingEntity;
import com.sgb.kjwl.view.ui.adapter.self.logistics.BargainingAdater;
import com.sgb.kjwl.viewmodel.self.logistics.BargainingModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;


/*
*   说明：我的议价 （物流运输  货主）md
* * */
public class BargainingActivity  extends AppActivity {


    @Inject
  public   BargainingModel viewModel;
    List<BargainingEntity> arrayList;

    //绑定的什么
    ActivityBargainingBinding binding;
    BargainingAdater bargainingAdater;

    //跳转操作
    public static void start(Context context) {
        Intent intent = new Intent(context, BargainingActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
       DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
        Log.e("CarriageBusiness","1");
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_bargaining);
        binding.include.titleView.setText("我的议价");
        binding.include.ivBack.setOnClickListener(this);
        if(bargainingAdater==null){
            bargainingAdater = new BargainingAdater(getActivity(), null);
        }
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.shape_list_divide));
        binding.recyclerView.addItemDecoration(divider);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(bargainingAdater);
        //刷新的一些操作
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getBargainingModel(false);

            }

            @Override
            public void onLoadMore() {
                viewModel.getBargainingModel(true);

            }
        });

        bargainingAdater.setOnItemClick(new AdapterOnItemClick<BargainingEntity>() {
            @Override
            public void onItemClick(BargainingEntity carriageManageEntity, int position) {

            }
        });

    }


    @Override
    protected void init() {
        super.init();
        //viewModel.myBargainingList(false);
    }


    @Override
    protected void onResume() {
        super.onResume();
        viewModel.getBargainingModel(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.right_view:
               // CarriageInfoPublishActivity.start(CarriageManageActivity.this);
                break;
        }
    }


    public void showBargaining(List<BargainingEntity> arrayList,boolean isMore) {

        this.arrayList = arrayList;
        if(!isMore) {
            binding.recyclerView.refreshComplete();
            binding.emptyView.setVisibility(arrayList.size() == 0 ? View.VISIBLE : View.GONE);
            bargainingAdater.setDatas(arrayList);
            binding.recyclerView.setLoadingMoreEnabled(true);
        }else{
            binding.recyclerView.loadMoreComplete();
            if (arrayList.size() > 0) {
                bargainingAdater.addDatas(arrayList);
            } else {
                binding.recyclerView.setLoadingMoreEnabled(false);
            }
        }

    }
}